package com.xmtlxx.web.controller.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.oms.service.IPropertyLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.oms.domain.PropertyLog;
import com.xmtlxx.oms.domain.bo.PropertyLogBo;
import com.xmtlxx.oms.domain.vo.PropertyLogVo;
import com.xmtlxx.oms.mapper.PropertyLogMapper;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 财物日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-11-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PropertyLogServiceImpl implements IPropertyLogService {

    private final PropertyLogMapper baseMapper;

    private final SysOssMapper sysOssMapper;

    /**
     * 查询财物日志
     *
     * @param id 财物日志主键
     * @return 财物日志
     */
    @Override
    public PropertyLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectPropertyLogVoByID(id);
    }

    /**
     * 查询财物日志列表
     *
     * @param bo 财物日志
     * @return 财物日志
     */
    @Override
    public TableDataInfo<PropertyLogVo> queryPageList(PropertyLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<PropertyLog> lqw = buildQueryWrapper(bo);
       // Page<PropertyLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<PropertyLogVo> result = baseMapper.selectPagePropertyLogList(pageQuery.build(), bo);
        if(result != null && result.getRecords() != null && result.getRecords().size() > 0){
            result.getRecords().forEach(item->{
                SysOssBo ossBo = new SysOssBo();
                ossBo.setBuss("propertyLog");
                ossBo.setObjid(item.getId());
                item.setOss(sysOssMapper.selectSysOssList(ossBo));
            });
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询财物日志列表
     *
     * @param bo 财物日志
     * @return 财物日志
     */
    @Override
    public List<PropertyLogVo> queryList(PropertyLogBo bo) {
       // LambdaQueryWrapper<PropertyLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectPropertyLogList(bo);
    }

    private LambdaQueryWrapper<PropertyLog> buildQueryWrapper(PropertyLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PropertyLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getObjid()), PropertyLog::getObjid, bo.getObjid());
        lqw.eq(bo.getLogTime() != null, PropertyLog::getLogTime, bo.getLogTime());
        lqw.eq(bo.getUserId() != null, PropertyLog::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), PropertyLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getLogType()), PropertyLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), PropertyLog::getContent, bo.getContent());
        lqw.eq(bo.getDeptId() != null, PropertyLog::getDeptId, bo.getDeptId());
        lqw.eq(bo.getReceiveDept() != null, PropertyLog::getReceiveDept, bo.getReceiveDept());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), PropertyLog::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PropertyLog::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), PropertyLog::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), PropertyLog::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增财物日志
     *
     * @param bo 财物日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PropertyLog insertByBo(PropertyLogBo bo) {
        PropertyLog add = BeanUtil.toBean(bo, PropertyLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            return add;
        }
        return null;
    }

    /**
     * 修改财物日志
     *
     * @param bo 财物日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(PropertyLogBo bo) {
        PropertyLog update = BeanUtil.toBean(bo, PropertyLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PropertyLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除财物日志
     *
     * @param ids 需要删除的财物日志主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PropertyLogVo selectPropertyLogVoByID(String id) {
        return baseMapper.selectPropertyLogVoByID(id);
    }

}
